package com.ang.quartz.normal.job;

import com.ang.quartz.normal.pojo.Data;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: ${description}
 * @author: ssang
 * @create: 2021/5/11 0011 15:30
 **/
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("===================定时任务开始执行："+sdf.format(new Date()));

        //获取JobDetail中的dataMap
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //获取dataMap中的字符串参数
        System.out.println(jobDataMap.getString("job_param"));
        //获取dataMap中的对象参数
        Data data = (Data) jobDataMap.get("job_data");
        System.out.println(String.format("定时任务启动：%s，并获取参数id：%s，flag：%s",
                jobExecutionContext.getJobDetail().getKey(),data.getId(),data.getFlag()));

        //获取trigger中的dataMap
        JobDataMap jobDataMap1 = jobExecutionContext.getTrigger().getJobDataMap();
        //获取dataMap中的字符串参数，并转换int
        System.out.println(jobDataMap1.getInt("job_trigger_param"));
        //获取dataMap中的对象参数
        Data data1 = (Data) jobDataMap1.get("trigger_data");
        System.out.println(String.format("定时任务启动：%s，并获取参数id：%s，flag：%s",
                jobExecutionContext.getTrigger().getKey(),data1.getId(),data1.getFlag()));
//        System.out.println(jobExecutionContext.getJobDetail().getJobDataMap().getInt(""));
    }
}
